<?php
/**
 *  date: 2017/7/10  11:11
 *  Author: L先生
 *  encoding:utf-8  language:PHP
 */

namespace Home\Model;


class ServeModel extends IndexBaseModel
{
    protected $tableName = 'answer_formalinfo';

    /**
     *申请列表
     * @param $title
     * @return array
     * @Date 201707
     * @Author Lin
     */
    public function getproposerList($name,$mobile,$status,$type)
    {
        if($name)
            $map['facilitator_name'] = array('like','%'.$name.'%');
        if ($mobile)
            $map['mobile'] = array('like','%'.$mobile.'%');
        if ($status)
            $map['status'] = $status;
        if ($type)
            $map['type'] = $type;
        $fqModel = M('facilitator_proposer');
        $count =$fqModel->where($map)->count();
        $Page = new \Think\Page($count,3);
        $show = $Page -> show();
        $result = $fqModel->alias('fp')
                        ->where($map)
                        ->field('fp.*,u.nickname')
                        ->join('LEFT JOIN __USERS__ u  ON u.id=fp.user_id ')
                        ->limit($Page->firstRow.','.$Page->listRows)
                        ->order('id desc')
                        ->select();
//        $sql = $fqModel->getLastSql();
        return array($result,$show,$sql);
    }


    /**
     *酒店列表
     * @param $title
     * @return array
     * @Date 201707
     * @Author Lin
     */
    public function gethotelList($orderby)
    {
        $map['status'] = 2;

        $orderby?$way = $orderby : 'asc';//默认低到高

        $order = array();
        $order['money'] = $way;
        $order['sort'] = 'asc';
        $order['id'] = 'desc';



        $hModel = M('hotel');
        $count =$hModel->where($map)->count();
        $Page = new \Think\Page($count,5);
        $show = $Page -> show();
        $result = $hModel->alias('h')
            ->where($map)
            ->field('h.*,u.nickname')
            ->join('LEFT JOIN __USERS__ u  ON u.id=h.user_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            //->order('money'.  $way,'sort asc,id desc')
            ->order($order)
            ->select();
        return array($result,$show);
    }


    /**
     *交通列表
     * @param $title
     * @return array
     * @Date 201707
     * @Author Lin
     */
    public function gettrafficList($orderby)
    {
        $map['status'] = 2;
        $orderby?$way = $orderby : 'asc';//默认低到高

        $order = array();
        $order['money'] = $way;
        $order['sort'] = 'asc';
        $order['id'] = 'desc';

        $hModel = M('traffic');
        $count =$hModel->where($map)->count();
        $Page = new \Think\Page($count,5);
        $show = $Page -> show();
        $result = $hModel->alias('t')
            ->where($map)
            ->field('t.*,u.nickname')
            ->join('LEFT JOIN __USERS__ u  ON u.id=t.user_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            //->order('id desc')
            ->order($order)
            ->select();
        return array($result,$show);
    }


    /**
     *交通列表
     * @param $title
     * @return array
     * @Date 201707
     * @Author Lin
     */
    public function getbaseList($name,$mobile,$status)
    {
        if($name)
            $map['base_name'] = array('like','%'.$name.'%');
        if ($mobile)
            $map['mobile'] = array('like','%'.$mobile.'%');
        if ($status)
            $map['status'] = $status;
        $hModel = M('base');
        $count =$hModel->where($map)->count();
        $Page = new \Think\Page($count,3);
        $show = $Page -> show();
        $result = $hModel->alias('t')
            ->where($map)
            ->field('t.*,u.nickname')
            ->join('LEFT JOIN __USERS__ u  ON u.id=t.user_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        return array($result,$show);
    }


    public function getUserHotelList($name,$mobile)
    {

        if ($mobile)
            $map['mt_user_hotel.mobile'] = array('like','%'.$mobile.'%');

        $hModel = M('user_hotel');
        $count =$hModel->where($map)->count();
        $Page = new \Think\Page($count,3);
        $show = $Page -> show();
        $result = $hModel
            ->where($map)
            ->field('mt_user_hotel.*,mt_users.nickname,mt_hotel.hotel_name')
            ->join('LEFT JOIN mt_users   ON mt_users.id=mt_user_hotel.user_id ')
            ->join('LEFT JOIN mt_hotel   ON mt_hotel.id=mt_user_hotel.hotel_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        return array($result,$show);
    }


    public function getUserBaselList($mobile)
    {
        if ($mobile)
            $map['mt_user_base.mobile'] = array('like','%'.$mobile.'%');
        $hModel = M('user_base');
        $count =$hModel->where($map)->count();
        $Page = new \Think\Page($count,3);
        $show = $Page -> show();
        $result = $hModel
            ->where($map)
            ->field('mt_user_base.*,mt_users.nickname,mt_base.base_name')
            ->join('LEFT JOIN mt_users   ON mt_users.id=mt_user_base.user_id ')
            ->join('LEFT JOIN mt_base   ON mt_base.id=mt_user_base.base_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        return array($result,$show);
    }









    public function deleteAllQuestion($idArr)
    {
        foreach($idArr['id'] as $vo)
        {
            M('question_bank')->where(array('id'=>$vo))->delete();
        }
        return true;
    }

    /**
     *答题信息
     * @Date 201707
     * @Author Lin
     */
    public function getAnswerInfoList($username,$school,$area,$city)
    {
        $map = array();
        if($username){
            $map['mt_answer_formalinfo.user_id'] = M('users')->where(array('nickname'=>array('like','%'.$username.'%')))->getField('id');
        }
        if($school)
            $map['mt_answer_formalinfo.school'] = array('like','%'.$school.'%');
        if($area)
            $map['mt_answer_formalinfo.area'] = array('like','%'.$area.'%');
        if($city)
            $map['mt_answer_formalinfo.city'] =array('like','%'.$city.'%');

        $order = array('score'=>'desc','total_time'=>'asc','id'=>'desc');

        $count = $this->where($map)->count();
        $Page = new \Think\Page($count,10);
        $show = $Page->show();

        $result = $this
            ->field('mt_answer_formalinfo.*,mt_users.nickname')
            ->where($map)
            ->join('LEFT JOIN mt_users  ON mt_users.id=mt_answer_formalinfo.user_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($order)
            ->select();
        return array($result,$show);
    }

    /**
     *全选删除信息
     * @param $idArr
     * @return bool
     * @Date 201707
     * @Author Lin
     */
    public function deleteAllData($idArr)
    {
        $model = M($idArr['model']);
        foreach($idArr['id'] as $vo)
            $model->where(array('id'=>$vo))->delete();
        return true;
    }















}